package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IPermissionService permissionService;

    @ResponseBody
    @RequestMapping("/empLogin")
    public JsonResult empLogin(String username, String password){
        JsonResult result = new JsonResult();
        Employee emp = employeeService.empLogin(username,password);
        UserContext.setCurrentUser(emp);
        List<Permission> permissionList = permissionService.selectByEmpId(emp.getId());
        UserContext.setCurrentPermissions(permissionList);
        return result;
    }

    @RequestMapping("/logout")
    private String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.html";
    }

    @RequestMapping("/nopermission")
    public String noPermission(){
        return "common/nopermission";
    }
    @RequestMapping("/error")
    public String error(){
        return "common/error";
    }
}
